package com.fatsatsuma.spreadsheets;

import java.io.InputStream;
import java.util.List;

import junit.framework.TestCase;

import org.apache.log4j.Logger;

/**
 * @author $Id: SpreadSheetReaderTestCase.java,v 1.1 2007/06/07 10:12:56 gevans
 *         Exp $
 */
public class SpreadSheetReaderTestCase extends TestCase {

	private Logger log = Logger.getLogger(this.getClass());

	public void testCanLoadValidFile() throws Exception {
		log.info("Testing Valid File...");
		try {
			assertNotNull("Unable to load inputstream", getValid());
			new SpreadsheetReader(getValid());
			assertTrue(true);
		} catch (InvalidWorkbookException exp) {
			log.error("Unable to load spreadsheet", exp);
			assertTrue(false);
		} catch (Exception exp) {
			log.fatal("Fatal Error", exp);
			throw exp;
		}
		log.info("Testing Valid File... Complete");
	}

	public void testCannotLoadInvalidFile() throws Exception {
		log.info("Testing Invalid File...");
		try {
			assertNotNull("Unable to load inputstream", getInvalid());
			new SpreadsheetReader(getInvalid());
			assertTrue(false);
		} catch (InvalidWorkbookException exp) {
			log.info("Correct Exception Thrown : " + exp.toString());
			assertTrue(true);
		} catch (Exception exp) {
			log.fatal("Fatal Error", exp);
			throw exp;
		}
		log.info("Testing Invalid File... Complete");
	}

	public void testWorksheetDoesExist() throws Exception {
		log.info("Testing Worksheet Does Exist...");
		try {
			assertNotNull("Unable to load inputstream", getValid());
			SpreadsheetReader reader = new SpreadsheetReader(getValid());
			assertTrue(true);
			assertNotNull(reader);
			List<String> headers = reader.getHeaders("Sheet1");
			assertNotNull(headers);
			log.info("Loaded Headers : " + headers);
			assertTrue(true);
		} catch (InvalidWorkbookException exp) {
			log.error("Unable to load spreadsheet", exp);
			assertTrue(false);
		} catch (WorksheetDoesNotExistException exp) {
			log.error("Unable to load worksheet", exp);
			assertTrue(false);
		} catch (Exception exp) {
			log.fatal("Fatal Error", exp);
			throw exp;
		}
		log.info("Testing Worksheet Does Exist... Complete");
	}

	public void testWorksheetDoesnotExist() throws Exception {
		log.info("Testing Worksheet Does Not Exist...");
		try {
			assertNotNull("Unable to load inputstream", getValid());
			SpreadsheetReader reader = new SpreadsheetReader(getValid());
			assertTrue(true);
			assertNotNull(reader);
			List<String> headers = reader.getHeaders("Sheet17");
			assertNotNull(headers);
			log.info("Loaded Headers : " + headers);
			assertTrue(false);
		} catch (InvalidWorkbookException exp) {
			log.error("Unable to load spreadsheet", exp);
			assertTrue(false);
		} catch (WorksheetDoesNotExistException exp) {
			log.info("Correct excepton thrown : " + exp.toString());
			assertTrue(true);
		} catch (Exception exp) {
			log.fatal("Fatal Error", exp);
			throw exp;
		}
		log.info("Testing Worksheet Does Not Exist... Complete");
	}

	private InputStream getValid() {
		return this.getClass().getClassLoader().getResourceAsStream(
				"spreadsheets/valid-spreadsheet.xls");
		// return this.getClass().getClassLoader().getResourceAsStream(
		// "spreadsheets/APPOINTMENT.XLS" );
	}

	private InputStream getInvalid() {
		return this.getClass().getClassLoader().getResourceAsStream(
				"spreadsheets/invalid-spreadsheet.xls");
	}

}
